"ui";
importClass(android.view.WindowManager);
importClass(android.view.View);
importClass(android.graphics.Color)
var storage = storages.create("testWechat");
var 循环次数 = parseInt(storage.get("循环次数", 10)) //一组数量
var 页面等待 = parseInt(storage.get("页面等待", 20)) //页面等待
var 飞行等待 = parseInt(storage.get("飞行等待", 2)) //页面等待
var 切号等待 = parseInt(storage.get("切号等待", 30)) //切号等待

var window = activity.getWindow();
var decorView = window.getDecorView();
var option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
decorView.getChildAt(0).getChildAt(1).getLayoutParams().height = device.height
//fd.setLayoutParams(lp)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
var androidId = device.getAndroidId()
var width = device.width
var height = device.height
var deviceInfo = width + "*" + height
var filesExit = files.exists("/sdcard/链接.txt") ? "总链接数" + getUrlSize() : "/内部存储/链接.txt 不存在"
ui.layout(
    <vertical bg="#000000" fitsSystemWindows="true">
        <text text="微信消息" w="*" h="50" textColor="green" gravity="center" textSize="20" />
        <text text="{{filesExit}}" w="*" h="auto" textColor="white" gravity="center" textSize="14" />
        <horizontal>
            <text text="循环次数:" w="80" h="*" gravity="center|right" textColor="red" />
            <input id="循环次数" text="{{循环次数}}" inputType="number" singleLine="true" textColor="white" w="80" h="*" />
        </horizontal>
        <horizontal>
            <text text="页面等待:" w="80" h="*" gravity="center|right" textColor="red" />
            <input id="页面等待" text="{{页面等待}}" inputType="number" singleLine="true" textColor="white" w="80" h="*" />
        </horizontal>
        <horizontal>
            <text text="飞行等待:" w="80" h="*" gravity="center|right" textColor="red" />
            <input id="飞行等待" text="{{飞行等待}}" inputType="number" singleLine="true" textColor="white" w="80" h="*" />
        </horizontal>
        <horizontal>
            <text text="切号等待:" w="80" h="*" gravity="center|right" textColor="red" />
            <input id="切号等待" text="{{切号等待}}" inputType="number" singleLine="true" textColor="white" w="80" h="*" />
        </horizontal>
        <Switch id="无障碍服务" text="无障碍服务" checked="{{auto.service != null}}" padding="8 0 0 8" textSize="15sp" textColor="white" />
        <Switch id="悬浮窗权限" text="悬浮窗权限" checked="{{floaty.checkPermission() != false}}" padding="8 0 0 8" textSize="15sp" textColor="white" />
        <button id="startBtn" style="Widget.AppCompat.Button.Colored" text="开始" />
        <button id="关闭" style="Widget.AppCompat.Button.Colored" text="关闭" />
        <text text="注意运行期间不要操作屏幕否则会影响屏幕点击！" w="*" h="auto" gravity="left" padding="8 0 0 8" textColor="red" textSize="20" />
    </vertical>
);

function debug_mode(_callback) {
    if (context.getPackageName() == 'org.autojs.autoxjs.v6') {
        try { _callback(); } catch (error) { }
        return true;
    }
    return false;
}

function getUrlSize() {
    var allData = null
    try { 
        var path = "/sdcard/链接.txt"; //txt文本路径
        var reg = /^\s+|\s+$/g; //匹配无效空白行
        var allData = files.read(path).replace(reg, "").replace("\r","").split("\n");
        return allData.length
    } catch (e) {

    }
    return 0
}



//安卓版本高于Android 9
log("我的引擎" + engines.myEngine())
engines.all().forEach(item => {
    if (item.id != engines.myEngine().id) {
        log("停止引擎" + item)
        item.forceStop()
    }

})
// let date = new Date().getTime()
// if (date >= 699026825000) {
//     toastLog("体验结束！")
//     exit()
// }
ui.无障碍服务.on("check", function (checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if (checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if (!checked && auto.service != null) {
        auto.service.disableSelf();
    }
});
ui.悬浮窗权限.on("check", function (checked) {
    //申请悬浮窗
    importClass(android.content.Intent);
    importClass(android.net.Uri);
    importClass(android.provider.Settings);
    var intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
        Uri.parse("package:" + context.getPackageName()));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    app.startActivity(intent);
});
// 当用户回到本界面时，resume事件会被触发
ui.emitter.on("resume", function () {
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.无障碍服务.checked = auto.service != null;
    ui.悬浮窗权限.checked = floaty.checkPermission() != false
});

ui.关闭.on("click", () => {
    engines.stopAllAndToast()
})
// var urls = files.read("/sdcard/链接.txt").split("\r\n")
// function extractUrls(text) {
//     const urlRegex = /https?:\/\/(?:www\.)?[^\s/$.?#].[^\s]*/g;
//     const urls = text.match(urlRegex);
//     return urls || [];
// }
// for (let index = 0; index < urls.length; index++) {
//     let url = urls[index];
//     if (url.indexOf("https://") != -1) {
//         let openUrl = extractUrls(url)
//         log("打开" + openUrl[0].replace("detail.1688.com", "m.1688.com"))
//     }

// }

ui.startBtn.on("click", () => {
    if (floaty.checkPermission() == false) {
        toast("请先开启悬浮窗权限！")
        return;
    }
    //程序开始运行之前判断无障碍服务
    if (auto.service == null) {
        toast("请先开启无障碍服务！");
        return;
    }

    threads.start(function () {
        try {
            var exists = files.exists("/sdcard/链接.txt")
            if(!exists){
                toastLog("请先用MT管理器查看 链接.txt文件是否正常")
                return
            }
            循环次数 = parseInt(ui.循环次数.text())
            页面等待 = parseInt(ui.页面等待.text())
            飞行等待 = parseInt(ui.飞行等待.text())
            切号等待 = parseInt(ui.切号等待.text())
            storage.put("循环次数", 循环次数)
            storage.put("页面等待", 页面等待)
            storage.put("切号等待", 切号等待)
            storage.put("飞行等待", 飞行等待)
            log("循环次数 " + 循环次数)
            log("页面等待 " + 页面等待)
            log("切号等待 " + 切号等待)
            log("飞行等待 " + 飞行等待)
            engines.execScriptFile("testwechat.js");
            engines.myEngine().forceStop()
        } catch (e) {
            toastLog("异常" + e)
            log(e)
        }
    });

})


